package com.zy.experience_system.controller;


import com.zy.experience_system.entity.StuInformation;
import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.ClassAndStuFrom;
import com.zy.experience_system.form.StuForm;
import com.zy.experience_system.service.ClassService;
import com.zy.experience_system.service.StuService;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.ResultVO;
import com.zy.experience_system.vo.StuAndClassVO;
import com.zy.experience_system.vo.StuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/student")
public class StuController extends BaseController {

    @Autowired
    private StuService stuService;
    @Autowired
    private ClassService classService;

    @GetMapping("/allTestSet/{stuId}")
    public ResultVO allTest(
            @PathVariable("stuId") String stuId) {
        try {
            return ResultUtil.success(stuService.findStuDataVO(stuId));
        } catch (Exception e) {
            return ResultUtil.error("没有记录");
        }
    }


    @GetMapping("/allStudent")
    public ResultVO allStudent() {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<StuInformation> list = stuService.findAll();
                List<Map<String, Object>> mapList = new ArrayList<>();

                for (StuInformation stuInformation : list) {
                    Map<String, Object> stuMap = new LinkedHashMap<>();
                    stuMap.put("id", stuInformation.getId());
                    stuMap.put("username", stuInformation.getStuId());
                    stuMap.put("name", stuInformation.getStuName());
                    mapList.add(stuMap);
                }
                return ResultUtil.success(mapList);
            } catch (Exception e) {
                return ResultUtil.error("没有记录");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);

    }

    @PostMapping("/create")
    public ResultVO create(@Validated @RequestBody StuForm stuForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                stuService.save(stuForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error(500, "创建失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PutMapping("/update")
    public ResultVO update(@Validated @RequestBody StuForm stuForm) {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                stuService.save(stuForm);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error(500, "修改失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/allStudentOfClass/{classId}")
    public ResultVO allStudentOfClass(@PathVariable("classId") Integer classId) {
        try {
            StuAndClassVO stuAndClassVO = stuService.findAllByClassId(classId);
            return ResultUtil.success(stuAndClassVO);
        } catch (Exception e) {
            return ResultUtil.error(500, "查找失败");
        }
    }

    @PostMapping("/addToClass/{classId}")
    public ResultVO addToClass(
            @Validated @RequestBody String students, @PathVariable("classId") Integer classId) {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                stuService.saveStuAndClass(students, classId);
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error(500, "添加失败");
            }

        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @DeleteMapping("/deleteFromClass")
    public ResultVO deleteFromClass(
            @Validated @RequestBody List<ClassAndStuFrom> classAndStuFromList) {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                for (ClassAndStuFrom classAndStuFrom : classAndStuFromList) {
                    stuService.deleteStuAndClass(classAndStuFrom);
                }
                return ResultUtil.success(null);
            } catch (Exception e) {
                return ResultUtil.error(500, "删除失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/allStuOfSet/{setId}")
    public ResultVO allStuOfSet(@PathVariable("setId") Integer setId) {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (setId != null) {
                try {
                    List<StuInfoVO> stuInfoVOList = stuService.findAllStuFromSet(setId);
                    return ResultUtil.success(stuInfoVOList);
                } catch (Exception e) {
                    return ResultUtil.error(500, "查询失败");
                }
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }


}
